
<script src='<?php echo DIR_JS?>ui.datepicker.js' type='text/javascript' language='javascript'> </script>

<div class="section" id="sitemaplist">
  <div class="section-title">Cập nhật kỳ báo đã giao</div>
  <div class="section-content padding1">
    <form name="frm" id="frm" action="<?php echo $action?>" method="post" enctype="multipart/form-data">
      <input type="hidden" id="diachi" name="diachi" value="" class="text" />
      <div class="button right">
        
        <input type="hidden" name="khachhangid" value="<?php echo $item['khachhangid']?>">
        <input type="hidden" name="id" value="<?php echo $item['id']?>">
      </div>
      <div class="clearer">^&nbsp;</div>
      <div id="error" class="error" style="display:none"></div>
      <div>
      	<table>
        	<tr class="left">
            	<td>      	
                    <label>Khu vực:</label> 
                    <select name="khuvucid" id="khuvucid">
                        <option value=""></option>
                        <?php       
                            foreach($dskhuvuc as $khuvuc)
                            { 
                        ?>
                        <option value="<?php echo $khuvuc['khuvucid']?>"> 
                        <?php echo $this->string->getPrefix("&nbsp;&nbsp;&nbsp;&nbsp;",$khuvuc['level'])?> 
                        <?php echo $khuvuc['tenkhuvuc']?> 
                        </option>
                        <?php 
                            }
                        ?>
                    </select>
				</td>
                <td>
                    <label>Nhân viên:</label>
                    <select id="nhanvienid" name="nhanvienid"></select>
                                    </td>
                                    <td>
                    <label>Loại báo:</label>
                    <select id="loaibaoid" name="loaibaoid">
                        <?php echo $loaibao ?>
                    </select>
				</td>
                <td>
					<label>Kỳ báo:</label> <select id="kybaoid" name="kybaoid"></select>
				</td>
            </tr>
			<tr class="clearer"><td></td></tr>
            <tr class="left">
            	<td>
                    <label>Ngày phát hành: từ</label>
                    <script language="javascript">
                        $(function() {
                            $("#tungay").datepicker({
                                changeMonth: true,
                                changeYear: true,
                                dateFormat: 'dd-mm-yy'
                            });
                            $("#denngay").datepicker({
                                changeMonth: true,
                                changeYear: true,
                                dateFormat: 'dd-mm-yy'
                            });
                        });
                    </script>
                    <input type="text" id="tungay" name="tungay" class="text" />
                    <label>đến:</label>
                    <input type="text" id="denngay" name="denngay" class="text" />
				</td>
                <td>
					<a class="button-other search" onClick="searchForm()">Tìm kiếm</a>
     			</td>
			</tr>
		</table>
                       

         

        <table class="data-table" width="100%">
          <thead>
            <tr>
              <th>STT</th>
              <th>Khách hàng</th>
              <th>Địa chỉ</th>
              <th>Tên kỳ báo</th>
              <th>Ngày phát hành</th>
              <th>Số lượng đặt</th>
              <th>Số lượng giao</th>
              <th>Nhân viên</th>
              <th>Thời gian giao</th>
              <th>Trạng thái</th>
              <th></th>
            </tr>
          </thead>
         
          <?php    
              
               
            	$index = 1;
            	foreach($datas as $item)
                {
                	if($item['hotennhanvien']!="")
                    {
            ?>
          <tbody>
            <tr>
              <td><?php echo $index++?></td>
              <td><?php echo $item['hoten'] ?></td>
              <td><?php echo $item['diachi'] ?></td>
              <td><?php echo $item['tenkybao'] ?></td>
              <td><?php echo $this->date->formatMySQLDate($item['ngayphathanh'])?></td>
              <td><?php echo $this->string->numberFormate($item['soluongdat']) ?></td>
              <td><?php echo $this->string->numberFormate($item['soluongdagiao']) ?></td>
              <td><?php echo $item['hotennhanvien'] ?></td>
              <td><?php echo $this->date->formatMySQLDate($item['ngaygiao'],"longdate")?></td>
              <!-- <td id="ngaygiao<?php echo $item['id']?>"><?php 
                        	if($item['trangthai'] =="dagiao")
                            { 
                            	echo $this->date->formatMySQLDate($item['ngaygiao'],"longdate");
                            }
                        ?></td>-->
              <td>
              <select class="trangthai" id="trangthai<?php echo $item['id']?>" name="trangthai">
                  <?php
                        foreach($trangthai as $key=>$val)
                        {
                        	
                    ?>
                  <option value="<?php echo $key ?>"><?php echo $val ?></option>
                  <?php 
                        }
                    ?>
                </select>
                <script language="javascript">
					$("#trangthai<?php echo $item['id']?>").val("<?php echo $item['trangthai']?>");
				</script>
              </td>
                <?php
                    $result = 0;
                    foreach($maxdates as $maxdate)
                    {
                        if($maxdate['loaibaoid']==$item['loaibaoid'] && 
                                $maxdate['ngayphathanh'] == $item['ngayphathanh'])
                        {
                            $result++;
                        }
                    }
                    
                    $today = $this->date->formatViewDate($this->date->formatMySQLDate($this->date->getToday()));
                    
                    if($result == 0 && $item['ngayphathanh'] < $today )
                    { 
                
                ?>
              <td>
              <input type="button" class="button confirm disabled btnConfirm" ref="<?php echo $item['id'] ?>"  value="Cập nhật" name="btnConfirm" id="update<?php echo $item['id']?>" disabled="disabled" />
               </td>
                    	<?php
                    
                    			}
                              	else
                                {
						?>
                <td>                 
                    <input type="button" class="button confirm btnConfirm" ref="<?php echo $item['id'] ?>"  value="Cập nhật" name="btnConfirm" id="btnConfirm<?php echo $item['id']?>" onClick="update" />
                 </td>
                    	<?php
                        		}
                                
                        ?>
              


              
            </tr>
          </tbody>
          <?php			}
          			}
            	
            ?>
        </table>
      </div>
    </form>
  </div>
</div>

<script language="javascript">

$(document).ready(function(){
		$('#loaibaoid').change(function(){
			$('#kybaoid').load('?route=deliman/capnhatkybaodagiao/getkybao&loaibaoid='+$('#loaibaoid').val(),function(){
				$("#kybaoid").val("<?php echo $_GET['kybaoid']?>");
			});
		});
		$('#loaibaoid').change();
	
		$('#khuvucid').change(function(){
		$('#nhanvienid').load('?route=deliman/capnhatkybaodagiao/getnhanvien&khuvucid='+$('#khuvucid').val(),function(){
				$("#nhanvienid").val("<?php echo $_GET['nhanvienid']?>");
			});
		});
		$('#khuvucid').change();
		$('.btnConfirm').click(function(){
			//alert($(this).attr('ref'));
			//Gui request len sever
			$.post("?route=deliman/capnhatkybaodagiao/updatethongtindatbao", 
			{
				id:$(this).attr('ref'),
				trangthai:$('#trangthai'+$(this).attr('ref')).val(),
			},
			
			function(data){
				//Hien thi ngay server tra ve
				alert('Cap nhat thanh cong');
				/*//alert(data);
				obj = jQuery.parseJSON(data);
				id=obj.id;
				trangthai = obj.trangthai;
				$('#trangthai'+id).val(trangthai);*/
		
		});			
	});
	
	$("#khuvucid").val("<?php echo $_GET['khuvucid']?>");
	
});
function update()
{
	$.blockUI({ message: "<h1>Please wait...</h1>" }); 
	$.post("?route=deliman/capnhatkybaodagiao/updatethongtindatbao", $("#frm").serialize(),
		function(data){
                            if(data == "true")
                            {
                                
                                window.location = "?route=deliman/khachhang";
                                
                            }
                            else
                            {
                            
                                $('#error').html(data).show('slow');
                                $.unblockUI();
                                
                            }
                      }
	);
}

function searchForm()
{
	var url ="?route=deliman/capnhatkybaodagiao";
		
	if($("#tungay").val() != "")
		url += "&tungay="+ $("#tungay").val();
	if($("#denngay").val() != "")
		url += "&denngay=" + $("#denngay").val();
	if($("#khuvucid").val() != "0")
		url += "&khuvucid=" + $("#khuvucid").val();
	if($("#nhanvienid").val()==null || $("#nhanvienid").val()=="0")
		 url+="nhanvienid="+$("#nhanvienid").val()
	if($("#nhanvienid").val() != "")
		url += "&nhanvienid=" + $("#nhanvienid").val();
	if($("#loaibaoid").val() != "")
		url += "&loaibaoid=" + $("#loaibaoid").val();
	if($("#kybaoid").val() != "")
		url += "&kybaoid=" + $("#kybaoid").val();
	
	window.location = url;
	$("#khuvucid").val("<?php echo $_GET['khuvucid']?>");
}

$("#tungay").val("<?php echo $_GET['tungay']?>");
$("#denngay").val("<?php echo $_GET['denngay']?>");

$("#khuvucid").val("<?php echo $_GET['khuvucid']?>");


$("#loaibaoid").val("<?php echo $_GET['loaibaoid']?>");

</script>